package org.algorithm.wE1.字符串;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class 最多只有2个字符的连续最大字串 {
    public static void main(String[] args) {
        String str = "abbaccc";
        System.out.println(lengthOfLongestSubstring(str));
    }

    public static int lengthOfLongestSubstring(String s) {
        if(s.length() == 0){
            return 0;
        }
        Map<Character,Integer> map = new HashMap<>();
        int left = 0;
        int max = 0;
        int len = s.length();
        for (int i = 0; i <len ; i++) {
            if(map.size()<3){
                map.put(s.charAt(i),i);
                left = Collections.min(map.values())+1;
            }else {
                map.remove(s.charAt(i));
            }
            max = Math.max(max,i-left+1);
        }
        return max;
    }
}
